//
//  CMDBNoteManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/6.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBNoteManager: NSObject {
    
    static let getInstance: CMDBNoteManager = CMDBNoteManager()
    
    private override init() {
        super.init()
        
    }
}

extension CMDBNoteManager: CMDBNoteManagerDelegate { }

// MARK: 获取 日记列表
extension CMDBNoteManager {
    
    // 获取网络notelist
    func queryNoteList(noteListParam: CMDBQueryNoteListParam,tableView: UITableView,handler: CMDBNoteListHandler,error: CMDBHttpErrorHandler,failure: CMDBHttpFailureHandler) throws {
        
        guard  noteListParam.type == "1" || noteListParam.type == "2" else {
            
            printLog(CMDBNoteControllerExceptionCodeType.UnSelectedDynamicType.rawValue)
            
            throw CMDBNoteControllerExceptionType.ExceptionCode(code: CMDBNoteControllerExceptionCodeType.UnSelectedDynamicType)
        }
        
        CMDBHttpManager.getInstance.request(noteListParam.URLString(), param: noteListParam.keyValues, ownPage: noteListParam.otype, tableView: tableView, success: { [weak self] (sysTime, data, page) in
            
            let arr = CMDBNoteBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBNoteBean>
            
            handler(noteList: arr)
            
            if let _ = arr where noteListParam.otype == 0 {
                
                self!.saveNoteList(arr!, identifier: "CMDBNoteList_\(CMDBApplicationManager.getInstance.account!.uid)")
            }
            }, error: error, failure: failure)
        
    }
    func saveNoteList(noteList: Array<CMDBNoteBean>, identifier: String) {
        
        let mutable = NSMutableArray()
        
        _ = noteList.map {
            
            mutable.addObject($0.keyValues!)
        }
        
        CMDBCacheDataManager.getInstance.saveArray(mutable, identifier: identifier)
        
    }
    func queryNoteListCache(identifier: String) throws -> Array<CMDBNoteBean> {
        
        guard let cache = CMDBCacheDataManager.getInstance.queryArray("CMDBNoteList_\(identifier)") else {
            
            printLog(CMDBNoteControllerExceptionCodeType.NoteListEmpty.rawValue)
            
            throw CMDBNoteControllerExceptionType.ExceptionCode(code: CMDBNoteControllerExceptionCodeType.NoteListEmpty)
        }
        
        return CMDBNoteBean.objectArrayWithKeyValuesArray(cache) as! Array<CMDBNoteBean>
    }
}
// MARK: 获取 日记详情
extension CMDBNoteManager {
    
    func queryNoteDetail(noteDetailParam: CMDBNoteDetailParam, handler: CMDBCommitNoteHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(noteDetailParam.URLString(), param: noteDetailParam.keyValues, success: { (sysTime, data, page) in
            
            }, error: error, failure: failure)
    }
}
extension CMDBNoteManager {
    func commitNote(commitNoteParam: CMDBCommitNoteParam, handler: CMDBCommitNoteHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !commitNoteParam.title.isEmpty else {
            
            printLog(CMDBNoteControllerExceptionCodeType.CommitNoteTitleEmpty.rawValue)
            
            throw CMDBNoteControllerExceptionType.ExceptionCode(code: CMDBNoteControllerExceptionCodeType.CommitNoteTitleEmpty)
        }
        guard !commitNoteParam.content.isEmpty else {
            
            printLog(CMDBNoteControllerExceptionCodeType.CommitNoteContentEmpty.rawValue)
            
            throw CMDBNoteControllerExceptionType.ExceptionCode(code: CMDBNoteControllerExceptionCodeType.CommitNoteContentEmpty)
        }
        
        CMDBHttpManager.getInstance.request(commitNoteParam.URLString(), param: commitNoteParam.keyValues, success: { (sysTime, data, page) in
            
            let note = CMDBNoteBean.objectWithKeyValues(data as! NSDictionary) as! CMDBNoteBean
            
            handler(noteBean: note)
            
            }, error: error, failure: failure)
    }
}

extension CMDBNoteManager {
    
    func saveSingleNote(noteBean: CMDBNoteBean, identifier: String) {
        
        
    }
    
    func querySingleNoteCache(identifier: String) throws -> CMDBNoteBean {
        
        return CMDBNoteBean()
    }
    func deleteSingleNoteCache(identifier: String) {
        
        
    }
}














